package com.example.choupaiapp.main.activity;

import android.Manifest;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.choupaiapp.R;
import com.example.choupaiapp.common.constant.Contacts;
import com.example.choupaiapp.common.finalokgo.callback.Callback;
import com.example.choupaiapp.common.finalokgo.request.RequestType;
import com.example.choupaiapp.common.ui.activity.CommonActivity;
import com.example.choupaiapp.common.util.SharedPreferencesHelp;
import com.example.choupaiapp.common.util.Utils;
import com.example.choupaiapp.main.bean.SplashBean;
import com.example.choupaiapp.main.constant.Url;
import com.hyphenate.chat.EMClient;
import com.lzy.okgo.model.Response;
import com.squareup.picasso.Picasso;

import butterknife.BindView;
import io.reactivex.functions.Consumer;

/**
 * Created by ASUS on 2019/10/25.
 */

public class SplashActivity extends CommonActivity {

    @BindView(R.id.image)
    ImageView image;
    @BindView(R.id.jump)
    TextView jump;
    Handler handler = new Handler();

    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            boolean isMainGuide = (boolean) SharedPreferencesHelp.get("cp_main_guide", false);
            if (isMainGuide) {
                startFinishActivity(MainActivity.class);
            } else {
                SharedPreferencesHelp.put("cp_main_guide", true);
                startFinishActivity(GuideActivity.class);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
            finishActivity();
            return;
        }
        // 隐藏状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_splash);

    }

    @Override
    protected void initView() {

        jump.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Utils.isDoubleClick()) {
                    return;
                }
                handler.removeCallbacks(runnable);
                boolean isMainGuide = (boolean) SharedPreferencesHelp.get("cp_main_guide", false);
                if (isMainGuide) {
                    startFinishActivity(MainActivity.class);
                } else {
                    SharedPreferencesHelp.put("cp_main_guide", true);
                    startFinishActivity(GuideActivity.class);
                }
            }
        });

        if (!(rxPermissions.isGranted(Manifest.permission.READ_EXTERNAL_STORAGE) && rxPermissions.isGranted(Manifest.permission.WRITE_EXTERNAL_STORAGE))) {
            rxPermissions.request(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE).subscribe(new Consumer<Boolean>() {
                @Override
                public void accept(Boolean aBoolean) throws Exception {
                    if (aBoolean) {

                    } else {
                        showImageToas("拒绝权限可能导致功能不能正常使用");
                    }
                }
            });
        }

        finalOkGo.request(RequestType.POST, Url.SPLASH, null, Url.SPLASH, new Callback<SplashBean>() {
            @Override
            public void onSuccess(SplashBean splashBean) {
                if (splashBean.getCode() == Contacts.SUCCESS) {
//                    Glide.with(context)
//                            .load(splashBean.getUrl())
//                            .placeholder(R.mipmap.splash)
//                            .crossFade()
//                            .into(image);
                    Picasso.get().load(splashBean.getUrl())
                            .placeholder(R.mipmap.splash)
                            .placeholder(R.mipmap.splash)
                            .into(image);
                    if (!TextUtils.isEmpty(splashBean.getJumpUrl())) {
                        image.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                if (Utils.isDoubleClick()) {
                                    return;
                                }
                                handler.removeCallbacks(runnable);
                                Bundle bundle = new Bundle();
                                bundle.putString("url", splashBean.getJumpUrl());
                                startFinishActivity(WebviewActivity.class, bundle);
                            }
                        });
                    }
                } else {
                    showToast(splashBean.getMsg());
                }
            }

            @Override
            public void onError(Response response) {
                super.onError(response);
            }
        });

//        EMClient.getInstance().groupManager().loadAllGroups();
        EMClient.getInstance().chatManager().loadAllConversations();
        handler.postDelayed(runnable, 5000);

    }

    @Override
    protected void initData() {
    }

}
